var Menu = function(options){
	this.init(options);
}

Menu.prototype = {
	init : function(options){
		//list:[{title:'',items:[{id:'',url:'',text:''}]}]
		this.id = options.id;
		this.targetContainer = options.targetContainer;
		this.menuNodes = options.menuNodes;
		this.expandNodes = options.expandNodes;
		if(!this.id || !this.targetContainer || !this.menuNodes){
			return;
		}
		
		var html = this.generateMenu();
		$(this.targetContainer).prepend(html);
		
		this.show();
		this.bindMenuNodeClickEvent();
		this.bindMenuLeafClickEvent();
	},
	
	generateMenu:function(){
		var menuNodes = this.menuNodes;
		var htmlArray = [];
		htmlArray.push('<div id="'+this.id+'" class="menu">');
		$.each(menuNodes,function(i,e){
			htmlArray.push('<div id="'+e.id+'"class="menu-node">');
			htmlArray.push('<div class="menu-node-status"><div class="status-medium hand-down"></div></div>');
			htmlArray.push('<div class="menu-node-title">'+e.title+'</div>');
			htmlArray.push('<ul class="menu-leafs">');
			var menuLeafs = e.items;
			$.each(menuLeafs,function(i,e){
				htmlArray.push('<li class="menu-leaf" id="'+e.id+'" url="'+e.url+'" >'+e.text+'</li>');
			});
			htmlArray.push('</ul>');
			htmlArray.push('</div>');
		});
		htmlArray.push('</div>');
		return htmlArray.join('');
	},
	show:function(){
		var me = this;
		var $menu = $('#'+this.id);

		var selectedId = this.getMenuCookie();
		if(selectedId){
			var $selectedItem = $menu.find('.menu-leafs [id='+selectedId+']').addClass('selected');
			var $selectedNode = $selectedItem.closest('.menu-node');
			me.showNode($selectedNode);			
		}
		
		var expandNodes = this.expandNodes;
		$.each(expandNodes,function(i,e){
			var $node = $('#'+e);
			me.showNode($node);
		});
	},
	showNode:function($node){
		$node.find('.menu-leafs').show();
		var $statusMedium = $node.find('.status-medium');
		$statusMedium.removeClass('hand-down').addClass('hand-up');
	},
	bindMenuNodeClickEvent:function(){
		$('#'+this.id).delegate('.menu-node-title','click',function(){
			var $menuLeafs = $(this).next();
			var $statusMedium = $menuLeafs.closest('.menu-node').find('.status-medium');
			var isMenuLeafsDisplayed = $menuLeafs.is(':visible');
			if(isMenuLeafsDisplayed){
				$menuLeafs.slideUp();
				$statusMedium.removeClass('hand-up').addClass('hand-down');
			}else{ 
				$menuLeafs.slideDown();
				$statusMedium.removeClass('hand-down').addClass('hand-up');
			}
		})
	},
	bindMenuLeafClickEvent:function(){
		var me = this;
		$('#'+this.id).delegate('.menu-leaf','click',function(){
			var url = $(this).attr('url');
			var id = $(this).attr('id');
			me.setMenuCookie(id);
			var isExist = url.indexOf('?')>=0;
			if(isExist){
				url += '&menuItem='+id;
			}else{
				url += '?menuItem='+id;
			}
			 window.location.assign(url);
		})
	},
	getMenuCookie:function(){
		return $.cookie('selectedMenuItem');
	},
	setMenuCookie:function(selectedMenuItem){
		var selectedMenuItemCookie = this.getMenuCookie();
		if(selectedMenuItem!=selectedMenuItemCookie){
			$.cookie('selectedMenuItem', selectedMenuItem, { expires: 7, path: '/' });			
		}
	}
};
